<?php
/**
 * Created by PhpStorm.
 * User: tandunzhao
 * Date: 2017/11/20
 * Time: 下午5:26
 */

namespace App\Console\Commands;

use App\Modules\Agent\Services\AgentServices;
use App\Modules\Channel\Services\ChannelDailyStatService;
use App\Modules\SendOrder\Models\SendOrder;
use App\Modules\Trade\Models\Order;
use App\Modules\Trade\Services\OrderService;
use DB;
use Illuminate\Console\Command;
use Log;

class SendOrdersMonthlyData extends Command
{
    /**
     * 执行命令 获取站点的派单数据及服务号信息
     *
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'update_monthly_send_orders_stat';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '更新服务号每月数据';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        \Log::info('start:更新服务号每月数据#####update_recent_three_month_data_start:');
        $this->updateRecentThreeMonthData();
        \Log::info('end:更新服务号每月数据#####update_recent_three_month_data_end:');
    }

    private function updateRecentThreeMonthData()
    {
        $start_date = date('Y-m-d', strtotime('-3 month'));
        $end_date = date('Y-m-d');
        $params = compact('start_date', 'end_date');//compact('distribution_channel_id', 'start_date', 'orderType', 'end_date', 'agent_name', 'orderBy', 'promotion_point');
        $data = ChannelDailyStatService::getMonthData($params, true);

        foreach ($data as $item) {

            $distribution_channel_id = $item->distribution_channel_id;
            $begin_time = $item->month . '-01';
            $end_time = date("Y-m-d 23:59:59", strtotime($begin_time . ' +1 month -1 day'));

            $item->totalCost = SendOrder::getDuringCost($distribution_channel_id, $begin_time, $end_time);
            $item->total_charge_amount = Order::getChannelChargeAmount($distribution_channel_id, $begin_time, $end_time);

            //代理商、推广渠道
            $agentInfo = AgentServices::getAgentByChannelId($item->distribution_channel_id);
            $item->agent_name = $agentInfo ? $agentInfo->agent_name : '';
            $item->promotion_channel = $agentInfo ? $agentInfo->agent_intro : '';
            $item->charge_amount_in_72_hours = Order::getChargeAmountDistributionChannelId($distribution_channel_id, $begin_time, $end_time, 3);
            $item->charge_amount_in_30_days = Order::getChargeAmountDistributionChannelId($distribution_channel_id, $begin_time, $end_time, 30);;

            $day_count = 3;
            $item->promotionRegUserRechargeAmountIn72hours = OrderService::getPromotionRegUserRechargeAmount(compact('distribution_channel_id', 'end_time', 'begin_time', 'day_count'));

            $day_count = 30;
            $item->promotionRegUserRechargeAmountInOneMonth = OrderService::getPromotionRegUserRechargeAmount(compact('distribution_channel_id', 'end_time', 'begin_time', 'day_count'));

            $day_count = 60;
            $item->promotionRegUserRechargeAmountInTwoMonth = OrderService::getPromotionRegUserRechargeAmount(compact('distribution_channel_id', 'end_time', 'begin_time', 'day_count'));

            $day_count = 90;
            $item->promotionRegUserRechargeAmountInThreeMonth = OrderService::getPromotionRegUserRechargeAmount(compact('distribution_channel_id', 'end_time', 'begin_time', 'day_count'));

            $to_insert = [
                'month' => $begin_time,
                'distribution_channel_id' => $item->distribution_channel_id,
            ];
            $to_update = [
                'cost' => $item->totalCost,
                'charge_amount' => $item->total_charge_amount,
                'promotionRegUserRechargeAmountIn72hours' => $item->promotionRegUserRechargeAmountIn72hours,
                'promotionRegUserRechargeAmountInOneMonth' => $item->promotionRegUserRechargeAmountInOneMonth,
                'charge_amount_in_72_hours' => $item->charge_amount_in_72_hours,
                'charge_amount_in_30_days' => $item->charge_amount_in_30_days,
                'promotionRegUserRechargeAmountInTwoMonth' => $item->promotionRegUserRechargeAmountInTwoMonth,
                'promotionRegUserRechargeAmountInThreeMonth' => $item->promotionRegUserRechargeAmountInThreeMonth,
                'total_subscribe_num' => $item->total_subscribe_num,
                'new_subscribe_num' => $item->total_new_subscribe_num,
                'promotion_point' => $item->promotion_point,
                'promotion_channel' => $item->promotion_channel,
                'agent_name' => $item->agent_name,
            ];
            DB::table('promotion_groups_monthly_send_orders')->updateOrInsert($to_insert, $to_update);
        }
    }
}